package com.cloud.service.impl;

import com.cloud.dao.NewsDao;
import com.cloud.dto.NewsDto;
import com.cloud.dto.UserDto;
import com.cloud.entity.News;
import com.cloud.service.NewsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NewsServiceImpl implements NewsService {
    @Autowired
    NewsDao newsDao;

    @Override
    public NewsDto SendNews(NewsDto newsDto){
        News news=new News();
        BeanUtils.copyProperties(newsDto,news);
        newsDao.save(news);
        return newsDto;
    }

    @Override
    public NewsDto setNews(String info, UserDto userDto){
        NewsDto newsDto=new NewsDto();
        Date curDate=new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=dateFormat.format(curDate);
        newsDto.setInfo(info);
        newsDto.setUserId(userDto.getUserId());
        newsDto.setTeamId(userDto.getTeamId());
        newsDto.setUserName(userDto.getName());
        newsDto.setTime(date);
        return newsDto;
    }

    @Override
    public List<NewsDto> getAllNews(Integer teamId){
        List<News> newsList=newsDao.findAllByTeamId(teamId);
        List<NewsDto> newsDtoList=new ArrayList<>();
        for(int i=0;i<newsList.size();i++){
            NewsDto newsDto=new NewsDto();
            News news=newsList.get(i);
            BeanUtils.copyProperties(news,newsDto);
            newsDtoList.add(newsDto);
        }
        return newsDtoList;
    }


}
